package SvnUser;
use lib "./Config-IniFiles-2.68/lib";
use Config::IniFiles;
use cfg;

my $svnadmin_ini=cfg::get('svnadmin_cfg');

sub read_cfg{
	$cfg_str='';
	open(FP,$svnadmin_ini) or die("No Config File");
	while($tmp=<FP>){
		$tmp=~s/\r\n$/\n/;
		$cfg_str.=$tmp;
	}
	close(FP);
	open(FP,'<',\$cfg_str);
	$cfg=new Config::IniFiles -file => *FP;
	return $cfg;
}

sub get_pwd {
	#
	# get_user ( uid )
	#
	$cfg=read_cfg();
	$pwd="";
	$pwd=$cfg->val("users","$_[0]");
	return $pwd;
}

sub chg_mypwd {
	#
	# chg_mypwd ( uid, old_pwd, new_pwd )
	#
	$uid=$_[0];
	$old_pwd=$_[1];
	$new_pwd=$_[2];

	$auth=get_pwd($uid);
	if($auth eq $old_pwd){
		set_pwd($uid,$new_pwd);
		return 1;
	}
	return 0;
}

sub set_pwd{
	#
	# set_pwd ( uid, new_pwd )
	#
	$uid=$_[0];
	$new_pwd=$_[1];
	$cfg=read_cfg();
	$cfg->setval('users',$uid,$new_pwd);
	$cfg->WriteConfig($svnadmin_ini);
}

sub get_user_list{
	#
	# get_user_list()
	#
	$cfg=read_cfg();
	return $cfg->Parameters("users");
}

sub add_user{
	#
	# add_user( User, PWD)
	#
	$cfg=read_cfg();
	$r=$cfg->exists("users","$_[0]");
	if($r==1){
		return 0;
	}else{
		$cfg->newval("users","$_[0]","$_[1]");
		$cfg->WriteConfig($svnadmin_ini);
		return 1;
	}
}

sub del_user{
	#
	# del_user( $uid )
	#
	$uid=$_[0];
	$cfg=read_cfg();
	$cfg->delval('users',$uid);
	$cfg->WriteConfig($svnadmin_ini);
}
